/*
 * JGO2RDF creates a RDF representation of GAF2 files, which are automatically sourced from GO ftp site.
 * Copyright (C) 2012 Matthew M. Hindle & Artem Lysenko
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package uk.co.jgo2rdf.rdf.refseq;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * @author hindlem
 *         Date: 4/21/11
 *         Time: 11:22 AM
 * @version 0.10
 */
public class RefSeqFactory {

    public static final String NCBI_URI = URIs.NCBI;

    private static final Model m_model = ModelFactory.createDefaultModel();

    private static final String LSRN = "http://purl.oclc.org/SADI/LSRN/";
    private static final String GOA = "http://unbsj.biordf.net/fishtox/record-translation-sadi-service-ontology.owl#";

    public static boolean idIsGenomic(String id) {
        for (String startCode : supportedRefSeqGenomicTypes)
            if (id.startsWith(startCode)) return true;
        return false;
    }

    public static boolean idIsRNA(String id) {
        for (String startCode : supportedRefSeqRNATypes)
            if (id.startsWith(startCode)) return true;
        return false;
    }

    public static boolean idIsProtein(String id) {
        for (String startCode : supportedRefSeqProteinTypes)
            if (id.startsWith(startCode)) return true;
        return false;
    }

    public static Set<String> supportedRefSeqTypes = new HashSet<String>(Arrays.asList(
            "NP", "NS", "ZP", "YP", "XR", "XP", "XM", "NZ",
            "NW", "NT", "NR", "NM", "NG", "NC", "AP", "AC"));

    public static Set<String> supportedRefSeqGenomicTypes = new HashSet<String>(Arrays.asList(
            "NS", "NZ", "NW", "NT", "NG", "NC", "AC"));

    public static Set<String> supportedRefSeqRNATypes = new HashSet<String>(Arrays.asList(
            "XR", "XM", "NR", "NM"));

    public static Set<String> supportedRefSeqProteinTypes = new HashSet<String>(Arrays.asList(
            "NP", "ZP", "YP", "XP", "AP"));

    public static Set<String> refSeqOntologyExceptions = new HashSet<String>(Arrays.asList(
            "AC", "AP", "NC", "NG", "NR", "NS", "NT", "NW", "NZ", "XM", "XP", "XR"
            , "YP", "ZP"
    ));

    private static Pattern refseqPattern;

    static {
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        for (String supportedRefSeqType : supportedRefSeqTypes) {
            sb.append(supportedRefSeqType);
            sb.append('|');
        }
        sb.replace(sb.length() - 1, sb.length(), ")");
        sb.append("_\\d*");
        refseqPattern = Pattern.compile(sb.toString());
    }

    public static void main(String[] a) {
        System.out.println(refseqPattern.pattern());
    }

    public static boolean isRefSeqId(String id) {
        return refseqPattern.matcher(id).matches();
    }

    public static Resource getRefSeqRecord(String type) throws RefSeqTypeUnsupportedException {
        if (type.indexOf('_') > -1) {
            type = type.substring(0, type.indexOf('_'));
        }
        String rtype = type.trim().toUpperCase();
        return m_model.createResource(LSRN + "NCBI_" + type + "_Record");
    }

    public static Resource getRefSeqIdentifier(String type) throws RefSeqTypeUnsupportedException {
        if (type.indexOf('_') > -1) {
            type = type.substring(0, type.indexOf('_'));
        }
        String rtype = type.trim().toUpperCase();
        return m_model.createResource(LSRN + "NCBI_" + type + "_Identifier");
    }

}
